/*
 * Copyright (c) 2001-2007, Inversoft, All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */
package org.inversoft.vertigo.fck.action;

import java.io.InputStream;
import java.io.StringWriter;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import org.inversoft.vertigo.fck.domain.Connector;
import org.inversoft.vertigo.fck.service.FCKService;
import org.texturemedia.smarturls.Result;
import org.texturemedia.smarturls.Results;

import com.google.inject.Inject;

/**
 * <p>
 * This class is an action that interfaces with the FCK file manager.
 * It uses the Vertigo configuration system to control where files
 * are located and how they can be managed.
 * </p>
 *
 * @author Brian Pontarelli
 */
@Results({
    @Result(name = "error", location = "", params = {"status", "500"}, type = "httpheader"),
    @Result(name = "success", location = "", params = {"contentType", "text/xml", "inputName", "resultStream"}, type = "stream")
})
public class FileManager extends Upload {
    private FileManagerCommand command;
    private String currentFolder;
    private String newFolderName;
    private InputStream result;

    @Inject
    public FileManager(FCKService fckService) {
        super(fckService);
    }

    public FileManagerCommand getCommand() {
        return command;
    }

    public void setCommand(FileManagerCommand command) {
        this.command = command;
    }

    public String getCurrentFolder() {
        return currentFolder;
    }

    public void setCurrentFolder(String currentFolder) {
        this.currentFolder = currentFolder;
    }

    public String getNewFolderName() {
        return newFolderName;
    }

    public void setNewFolderName(String newFolderName) {
        this.newFolderName = newFolderName;
    }

    public InputStream getResultStream() {
        return result;
    }

    public String execute() {
        // If this is an upload, skip processing and handle control over to the upload action
        if (command == FileManagerCommand.FileUpload) {
            String result = super.execute();
            if (result.equals(SUCCESS)) {
                return "upload-success";
            }

            return result;
        }

        // Otherwise, process the request according to the command
        Connector connector = null;
        if (command == FileManagerCommand.CreateFolder) {
            connector = fckService.createFolder(currentFolder, newFolderName, getType());
        } else if (command == FileManagerCommand.GetFolders) {
            connector = fckService.getFolders(currentFolder, getType());
        } else if (command == FileManagerCommand.GetFoldersAndFiles) {
            connector = fckService.getFoldersAndFiles(currentFolder, getType());
        }

        try {
            JAXBContext context = JAXBContext.newInstance(Connector.class);
            Marshaller marshaller = context.createMarshaller();
            StringWriter sw = new StringWriter();
            marshaller.marshal(connector, sw);

            String xml = sw.toString();
            this.result = new ByteArrayInputStream(xml.getBytes("UTF-8"));
        } catch (JAXBException e) {
            // Bad error!
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            // Bad error!
            throw new RuntimeException(e);
        }

        return SUCCESS;
    }
}